﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;

namespace R10App
{
    class MapLoader
    {
        private Bitmap map;
        private double Lat1;
        private double Lon1;
        private double Lat2;
        private double Lon2;

        public bool LoadMap(string filename)
        {
            try
            {
                map = (Bitmap)Bitmap.FromFile(filename + ".bmp");

                StreamReader str = File.OpenText(filename + ".txt");
                string l1 =str.ReadLine();
                string[] toks = l1.Split(' ', '\t');
                Lat1 = double.Parse(toks[0]);
                Lon1 = double.Parse(toks[1]);
                
                string l2 = str.ReadLine();
                toks = l2.Split(' ', '\t');
                Lat2 = double.Parse(toks[0]);
                Lon2 = double.Parse(toks[1]);

                str.Close();
            }
            catch
            {
                return false;
            }
            return true;
        }

        public void Pix2LL(int pX, int pY, out double Lat, out double Lon )
        {
            double LonScale = (Lon2 - Lon1) / map.Width;
            Lon = Lon1 + pX * LonScale;

            double LatScale = (Lat2 - Lat1) / map.Height;
            Lat = Lat1 + pY * LatScale;
        }

        public void LL2Pix(double Lat, double Lon, out int pX, out int pY)
        {
            double LonScale = (Lon2 - Lon1) / map.Width;
            pX = (int)((Lon - Lon1) / LonScale);

            double LatScale = (Lat2 - Lat1) / map.Height;
            pY = (int)((Lat - Lat1) / LatScale);
        }

        public Bitmap Getmap()
        {
            return map;
        }
    }
}
